//
//  RealNameViewController.m
//  OneCreditTreasure
//
//  Created by iOS Grouper on 16/8/25.
//
//

#import "RealNameViewController.h"
#import "CertificationViewController.h"
#import "CarInfoTableViewCell.h"
#import "CertificationViewController.h"

@interface RealNameViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) UIButton *confirmButton;

@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *pwdTF;
@end

@implementation RealNameViewController
#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 100) {
        [self.pwdTF becomeFirstResponder];
    } else {
        [self.pwdTF resignFirstResponder];
    }
    return YES;
}

#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Cell.leftLB.text = self.titleArray[indexPath.row];
    [Cell.infoTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(kScreenW / 2 - 30 * AUTO_WIDTH);
        make.top.equalTo(4);
        make.bottom.equalTo(-4);
        make.right.equalTo(-12);
    }];
    Cell.infoTF.placeholder = (indexPath.row == 0 ? @"请输入您的姓名" : @"请输入您的身份证号码");
    Cell.infoTF.tag = (indexPath.row == 0 ? 100 : 101);
    Cell.infoTF.delegate = self;
    
    if (indexPath.row == 0) {
        self.nameTF = Cell.infoTF;
        Cell.infoTF.returnKeyType = UIReturnKeyNext;
    } else {
        self.pwdTF = Cell.infoTF;
    }
    return Cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableView];
    [self confirmButton];
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"实名认证";
    }
    return self;
}

#pragma mark - 方法 methods
- (void)confirmMethod:(UIButton *)sender {
    [[NSUserDefaults standardUserDefaults] setBool:([self.nameTF.text isEqualToString:@"test"] && [self.pwdTF.text isEqualToString:@"123456"]) forKey:@"认证信息"];
    
    CertificationViewController *vc = [CertificationViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载 lazy loading
- (UIButton *)confirmButton {
    if(_confirmButton == nil) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_confirmButton];
        [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.height.equalTo(44);
        }];
        _confirmButton.backgroundColor = kRGBA(0, 0, 0, 0.8);
        [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(confirmMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kRGBA(234, 234, 234, 1);
        
        [_tableView registerClass:[CarInfoTableViewCell class] forCellReuseIdentifier:@"Cell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSArray *)titleArray {
    if(_titleArray == nil) {
        _titleArray = @[@"输入姓名",
                        @"输入身份证号码"];
    }
    return _titleArray;
}

@end
